from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Users(db.Model):
    user = db.UserProperty()
    created = db.DateTimeProperty(auto_now = True)
    updated = db.DateTimeProperty(auto_now_add = True)

class Log(db.Model):
    msg = db.TextProperty()
    created = db.DateTimeProperty(auto_now=True)
    level = db.StringProperty(default='info')

class MainPage(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            Users.get_or_insert('u_'+user.nickname(), user=user)
        else:
            Log(msg='User is None').put()
        
        self.response.headers['Content-Type'] = "application/xml"
        self.response.out.write("""<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs title="Test for Google Account" 
      description="if it works, all your base belongs to us :-)"
      thumbnail=""
      screenshot=""
      author_email="pranny@gmail.com">
    <Require feature="opensocial-0.8"/>
  </ModulePrefs>
  <Content type="html">
    <![CDATA[
    <!-- ALm6fM3dpUaqryfRfRvMMVVBRE43gLP+z/tDZzJMuHUa96fOs1R9ByHuKNVV5ZXmS/770b1pJor8eTk87F9rc1oIUTSIxX6+/al7bR0SdQ1X57cSztoAGKnMNs1S/hEXMtl0fGUIiOOm -->
		<h1>hello World!</h1>
    ]]>
  </Content>
</Module>""")


application = webapp.WSGIApplication([('/', MainPage)], debug=True)


def main():
    run_wsgi_app(application)


if __name__ == "__main__":
    main()
